<template>
  <div class="login-container">
    <!-- 
      element-plus
        el-form 是element-plus 提供的表单组件
          el-form-item 表单项 el-from 可以有多个表单项
          model : 代表表单元素收集的数据收集到哪里
          rules : 校验规则
     -->
    <el-form ref="formRef" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <!-- 登录页面的标题 -->
      <div class="title-container">
        <h3 class="title">尚品汇后台管理</h3>
      </div>
      <!-- 账号表单项 -->
      <el-form-item prop="username">
        <!-- icon图标 -->
        <span class="svg-container">
          <svg-icon name="ele-OfficeBuilding" />
        </span>
        <!--输入账号的表单元素组件-->
        <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text"  />
      </el-form-item>

      <!-- 密码表单项 -->
      <el-form-item prop="password">
        <!-- icon图标 -->
        <span class="svg-container">
          <svg-icon name="ele-Lock" />
        </span>
        <!--输入密码的表单元素-->
        <el-input ref="passwordRef" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" name="password" @keyup.enter="handleLogin" />
        <!-- 添加了点击事件,点击小图标根据文本内容变化,显示哪种样式的小眼睛 -->
        <span class="show-pwd" @click="showPwd">
          <svg-icon :name="passwordType === 'password' ? 'ele-Hide' : 'ele-View'" />
        </span>
      </el-form-item>
      <!-- 点击登录 -->
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;height: 40px;" @click.prevent="handleLogin">登 陆</el-button>
    </el-form>
  </div>
</template>

<script lang="ts">
export default {
  name: 'Login'
}
</script>

<script lang="ts" setup>
import { nextTick, ref, watch } from 'vue'
// ts语法
import type { FormInstance } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'

// 小仓库
import { useUserInfoStore } from '@/stores/userInfo'
const userInfoStore = useUserInfoStore()
// 路由route
const route = useRoute()
// 路由器router
const router = useRouter()
//表单信息  默认信息
const loginForm = ref({
  username: 'admin',
  password: '111111'
})
//控组登录按钮是否有加载效果
const loading = ref(false)
// 动态文本,小眼睛状态通过文本内容改变
const passwordType = ref('password')
// 想去却没有去的路由
const redirect = ref('')
//密码的组件的VC
const passwordRef = ref<HTMLInputElement>()
//获取el-form表单组件实例
const formRef = ref<FormInstance>()

//校验规则定制
const validateUsername = (rule: any, value: any, callback: any) => {
  /* 
  rule:校验规则对象
  value :输入的内容
  callback :回调函数 ,校验规则通过调用次函数,如果校验规则没通过，返回错误提示信息
  */
 //长度小于4,提示错误信息
  if (value.length < 4) {
    callback(new Error('用户名长度不能小于4位'))
  } else {
    callback()
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}
//表单校验:校验用户名与密码规则
//分别校验用户名与密码
const loginRules = {
  //数组里面每一个对象:代表一个校验规则
  //required:代表用户名务必校验
  //validator:校验规则
  //trigger:触发校验规则的实际 change(文本发生变化)|blur(失却焦点)
  username: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

//切换密码的显示/隐藏 小眼睛状态
const showPwd = () => {
  passwordType.value = passwordType.value === 'password'?'text' : 'password'
  // 点击小眼睛,让密码框聚焦
  nextTick(() => {
    passwordRef.value?.focus()
  })
}

//监听路由变化
watch(
  route,
  () => {
    // 当前路由是否有想去的路由,如果有就存储起来
    redirect.value = route.query && (route.query.redirect as string)
  },
  // 立即监视
  { immediate: true }
)



/* 
点击登陆的回调
*/
const handleLogin = async () => {
  // 返回一个promise对象   如果表单内容全部校验成功,返会成功
  await formRef.value?.validate()

  //login效果开启
  loading.value = true
  const { username, password } = loginForm.value
  try {
    // 调用小仓库传参发请求
    await userInfoStore.login(username, password)
    // 返回promise 成功跳转路由,先查看是否有想去的路由
    router.push({ path: redirect.value || '/' })
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  background-image: url(../../assets/bg.jpg);
  background-color: #2d3a4b;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    .el-input__wrapper {
      width: 100%;
      background-color: transparent;
      box-shadow: none;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
